/*
 * Filename : SOCFileWriter.java
 * Package : com.spi.soconversion
 * Created by: Software Paradigms International Group, LLC. (SPI) 
 * Copyright (c) 2010 HBC
 */

package com.spi.soconversion.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import com.spi.soconversion.base.SOCException;



/**
  * This class provides method for writing reporting data into a file 
  * in Sales Order Conversion Application.
  * <p>
  * Issues:
  * <p>
  * History:
  * <p>
  * <ul>
 * <li>2010.04.17 pk: created
 * </ul>
 *
 * @author Priya Khanna, SPI (pk)
 */

public class SOCFileWriter {

    private static final String XML = "xml";
	private HashMap<String,BufferedOutputStream> fileMap = new HashMap<String,BufferedOutputStream>();

	/**
	 * This method returns Buffered writer for the input file.
	 * @param filename - File name represents file to be opened for writing
	 * @return BufferedWriter - Buffered Writer for input file
	 * @throws SOCException - Wraps up all the Exceptions encountered.
	 */
    public BufferedOutputStream openFileToWrite(String fileName) throws SOCException
    {
        BufferedOutputStream result = null;
        if (fileMap.get(fileName) == null)
        {
            File newFile = new File(fileName);
            try
            {
                if (newFile.exists())
                {
                    newFile.delete();
                    newFile = null;
                }
            }
            catch (SecurityException se)
            {
                throw new SOCException(
                		ErrorMessageReader.getError(
                				ErrorConstants.FAILED_FILE_DELETE)+
                				SOCConstants.BLANK_STRING+
                				SOCConstants.HYPHEN+
                				SOCConstants.BLANK_STRING+
                				fileName,se);
            }
            try
            {
                result =  new BufferedOutputStream(new FileOutputStream(fileName, true));
                if(fileName.endsWith(XML))
                {
                	result.write(SOCConstants.XML_FILE_HEADER.getBytes());
                    result.write(SOCConstants.NEW_LINE.getBytes());
                }
                fileMap.put(fileName, result);
            }
            catch (IOException ioe)
            {
                throw new SOCException(
                		ErrorMessageReader.getError(
                				ErrorConstants.FAILED_FILE_CREATE)+
                				SOCConstants.BLANK_STRING+
                				SOCConstants.HYPHEN+
                				SOCConstants.BLANK_STRING+
                				fileName,ioe);
            }
        }
        else
        {
            result = fileMap.get(fileName);
        }
        return result;
    }
   
	/**
	 * This method closes Buffered writer for the input file.
	 * @param filename - File name represents file to be closed for writing
	 * @throws SOCException - Wraps up all the Exceptions encountered.
	 */
    public void closeFile(String fileName) throws SOCException
    {
    	BufferedOutputStream writer =  fileMap.get(fileName); 
        if (writer != null)
        {
            try
            {
                writer.close();
                fileMap.remove(fileName);
            }
            catch (IOException ioe)
            {
                throw new SOCException(
                		ErrorMessageReader.getError(
                				ErrorConstants.FAILED_FILE_CLOSE)+
                				SOCConstants.BLANK_STRING+
                				SOCConstants.HYPHEN+
                				SOCConstants.BLANK_STRING+
                				fileName,ioe);
            }
        }
    }
    
	/**
	 * This method writes data into xml file.
	 * @param filename - File name represents file where data to be written
	 * @param data - Holds data to be written in file
 	 * @throws SOCException - Wraps up all the Exceptions encountered.
	 */
	public void write(String fileName, String data) throws SOCException
	{
		if (data != null)
		{
			try
			{
				BufferedOutputStream fileWriter = openFileToWrite(fileName);
                fileWriter.flush();
                fileWriter.write(data.getBytes("UTF-8"));
                fileWriter.flush();
			}
			catch (IOException ioe)
			{
				  throw new SOCException(
	                		ErrorMessageReader.getError(
	                				ErrorConstants.FAILED_FILE_WRITE)+
	                				SOCConstants.BLANK_STRING+
	                				SOCConstants.HYPHEN+
	                				SOCConstants.BLANK_STRING+
	                				fileName,ioe);
			}
		}
	}
	
}